package com.wica.adapter;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import com.wica.view.ELSearch;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;

public class ELAPersonalizedSearch extends EfficientListAdapter {	
	
	// *************** Constants ************** //
    // Set up a random unique ID for message handler
    protected static final int GUIUPDATEIDENTIFIER = 12345;
	// Log constant
	// private static final String LOG_TAG = "__FT.ELAPersonalizedSearch__";

	// *************** Attributes ************************* //  
    /** numberOfResults: Integer -- 0..30 (regarding FaceTube WebService)**/
	private Integer numberOfResults;
	private String searchString;
	private Integer userID;
//	private ELSearch list;
//	private ELAPersonalizedSearch adapter;	
    
    // *************** Methods **************************** //
	public ELAPersonalizedSearch(ELSearch list,String searchString) {
		super(list);
		this.adapter=this;
		this.list=list;
		this.numberOfResults=Integer.valueOf(PreferenceManager.getDefaultSharedPreferences((Context)list).getString("numberOfResults_search_preference", "30"));
		this.searchString=searchString;
        this.userID=PreferenceManager.getDefaultSharedPreferences((Context)list).getInt("userID", -1);		
		if (!internetNetworkAvailable())
			this.searchString="__empty";
        getListOfVideosThread();
    	Toast.makeText((Context)list, "More options by pressing MENU button", Toast.LENGTH_LONG).show();
	}//EfficientListAdapterMostViewed method	
	
//    // Do lengthy operations in a child thread
//    protected void getListOfVideosThread(){	
//    	// Start a progress dialog here        
//        mypd = ProgressDialog.show(	(Context)list, 
//        							"Searching", 
//        							"Please wait...", 
//        							false);        
//        Thread t = new Thread() {			        
//        	public void run() {				
//        		getListOfVideosUnthreaded();    		        		    
//        		// Send a message to the handler                    
//        		Message message = new Message();                  
//        		message.what = ELAPersonalizedSearch.GUIUPDATEIDENTIFIER;                    
//        		ELAPersonalizedSearch.this.myGUIUpdateHandler.sendMessage(message);			
//        	}
//        };     	              
//        t.start(); 	
//    }// getListOfVideosThread method
	
//	protected void getListOfVideosUnthreaded(){
//		// Local Variables
//		int i;
//		URL url;
//		InputStream is;
//		// we call the service
//		getListOfVideosMethod();
//		// we create vectors with both data and thumbnails
//		// we do not invoke a virtual method to enhance performance
//		for (i=0;i<listOfMovies.size();i++){
//			data.add(listOfMovies.get(i).getTitle());
//			try {
//				url = new URL(listOfMovies.get(i).getURLThumbnail());
//				is = (InputStream) url.getContent();
//				bmpd.add((BitmapDrawable) BitmapDrawable.createFromStream(is , "youtube"));	            
//			} catch (MalformedURLException e) {
//				Log.e(LOG_TAG,e.toString());
//			} catch (IOException e) {
//				Log.e(LOG_TAG,e.toString());
//			}
//		}//for
//    }// getListOfVideosUnthreaded method
    	
    protected void getListOfVideosMethod() {
    	super.getListOfVideosMethod();
		listOfMovies = fts.getPersonalizedSearchClips(searchString, numberOfResults, userID);
		Log.v(LOG_TAG, LOG_TAG+" "+listOfMovies.size());
	}// getListOfVideosMethod method
	
//    // Set up the message handler
//    Handler myGUIUpdateHandler = new Handler() {
//    	
//    	@Override
//    	public void handleMessage(Message msg) {
//    		switch (msg.what) {
//    			case ELAPersonalizedSearch.GUIUPDATEIDENTIFIER: 	    
//    				list.setListAdapter(adapter);
//    				mypd.dismiss();
//    				break;
//    			default:
//    				break;	    		
//    		}
//	    	super.handleMessage(msg);       
//    	}// handleMessage method  
//    };

    // *************** Set methods ************** //  
	public void setNumberOfResults(Integer numberOfResults){
    	this.numberOfResults = numberOfResults;
    }// setNumberOfResults method	
    
    // *************** Get methods ************** //  
	public Integer getNumberOfResults(){
    	return(this.numberOfResults);
    }// getNumberOfResults method	

}// ELAPersonalizedSearch class